﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Interactivity;
using System.Windows.Data;

namespace XWord.WPF.ApplicationFramework
{
    public class WindowInitBehavior : Behavior<Window>
    {
        protected override void OnAttached()
        {
            var window = Application.Current.MainWindow;
            window.SetBinding(Window.TopmostProperty, new Binding("IsTopmost") { Source = XWordAppConfig.Current });
            window.Closing += new System.ComponentModel.CancelEventHandler(window_Closing);
            //window.StateChanged += new EventHandler(window_StateChanged);
            base.OnAttached();
        }

        void window_StateChanged(object sender, EventArgs e)
        {
            var window = Application.Current.MainWindow;
            if (window.WindowState == WindowState.Minimized)
            {
                Application.Current.ForceGC();
                window.ShowInTaskbar = false;
            }
            else
            {
                window.ShowInTaskbar = true;
            }
        }

        void window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (XWordAppConfig.Current.CloseWindowMode == CloseWindowMode.Mini)
            {
                e.Cancel = true;
                Application.Current.MainWindow.WindowState = WindowState.Minimized;
            }
        }
    }
}
